package com.gopay.common.rcs.vo;

import com.gopay.common.constants.rcs.TranDirection;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.BaseEntity;
import com.gopay.common.domain.acps.AcctProd;
import com.gopay.common.domain.solution.TranLimitRes;
import com.gopay.common.util.DateUtils;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;

public class RcsTransLimitView extends BaseEntity{

	private static final long serialVersionUID = -5217516905702316558L;
	
	
	private TranLimitRes tranLimitRes;
	private TranDirection tranDirection;
	private AcctProd acctProd;
	private String acctId;
	private IntTxnCd intTxnCd;
	private String custId;
	
	
	public TranLimitRes getTranLimitRes() {
		return tranLimitRes;
	}
	public void setTranLimitRes(TranLimitRes tranLimitRes) {
		this.tranLimitRes = tranLimitRes;
	}
	public TranDirection getTranDirection() {
		return tranDirection;
	}
	public void setTranDirection(TranDirection tranDirection) {
		this.tranDirection = tranDirection;
	}
	public AcctProd getAcctProd() {
		return acctProd;
	}
	public void setAcctProd(AcctProd acctProd) {
		this.acctProd = acctProd;
	}
	public String getAcctId() {
		return acctId;
	}
	public void setAcctId(String acctId) {
		this.acctId = acctId;
	}
	public IntTxnCd getIntTxnCd() {
		return intTxnCd;
	}
	public void setIntTxnCd(IntTxnCd intTxnCd) {
		this.intTxnCd = intTxnCd;
	}
	public String getCustId() {
		return custId;
	}
	public void setCustId(String custId) {
		this.custId = custId;
	}
	
	/**
	 * 单日累计限额
	 * @return
	 */
	public BigDecimal getDayLimit(){
		if(tranLimitRes!=null && checkAmtLimit(tranLimitRes.getDayLimit()) && checkValidDate()){
			return tranLimitRes.getDayLimit();
		}
		return null;
	}
	
	/**
	 *  单笔限额
	 * @return
	 */
	public BigDecimal getOnceLimit(){
		if(tranLimitRes!=null && checkAmtLimit(tranLimitRes.getOnceLimit()) && checkValidDate()){
			return tranLimitRes.getOnceLimit();
		}
		return null;
	}
	
	
	/**
	 *  单日累计次数
	 * @return
	 */
	public long getDayTimes(){
		if(tranLimitRes!=null && checkValidDate()){
			return tranLimitRes.getDayTimes();
		}
		return 0;
	}
	
	
	private boolean checkAmtLimit(BigDecimal amt){
		if(tranLimitRes.isEnabled() && amt!=null && amt.compareTo(new BigDecimal(0))==1){
			return true;
		}
		return false;
	}
	
	private boolean checkValidDate(){
		boolean check = true;
		if(tranLimitRes.isEnabled() && ((StringUtils.isNotBlank(tranLimitRes.getStartDate())) && (StringUtils.isNotBlank(tranLimitRes.getEndDate())) && tranLimitRes.getEndDate().length()==8 && tranLimitRes.getStartDate().length()==8 )){
    		int cur = Integer.parseInt(DateUtils.getCurrDate("yyyyMMdd"));
    		int sat = Integer.parseInt(tranLimitRes.getStartDate());
    		int end = Integer.parseInt(tranLimitRes.getEndDate());
    		if(!(cur>=sat && cur<=end)){
    			check = false;
    		}
    	}
		return check;
	}
	
}
